﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace  com.lib.manager
{
   
    /// <summary>
    /// 音频管理器
    /// </summary>
   public  class AudioManager:MonoBehaviour
   {
       public static AudioManager Instance = null;

       private List<AudioSource> src = new List<AudioSource>();

       private Dictionary<string, AudioClip> clipdata = new Dictionary<string, AudioClip>();

       private bool canPlay = true;

       private int vol = 1;

       public int _vol
       {
           get
           {
               return vol;
           }
           set
           {
               vol = value > 1 ? 1 : value;
               vol = value < 0 ? 0 : value;
               if (vol > 0)
               {
                   PlayAll();
               }
               else
               {
                   StopAll();
               }
           }
       }

       public bool _CanPlay
       {
           get
           {
               return canPlay;
           }
           set
           {
               if (value)
               {
                   PlayAll();
               }
               else
               {
                   StopAll();
               }
               canPlay = value;
           }
       }

       private void PlayAll()
       {
           if (src[0].audio.clip != null)
           {
               src[0].audio.volume = vol;
               src[0].audio.Play();
              
           }

       }

       private void StopAll()
       {
           if (src[0].audio.clip != null)
           {
               src[0].audio.volume = vol;
               src[0].audio.Stop();
           }

       }
        
       void Awake()
       {
           Instance = this;
           Init();
           DontDestroyOnLoad(this.gameObject);
       }

       /// <summary>
       /// 初始化
       /// </summary>
       private void Init()
       {
           for (int i = 0; i < 10; i++)
           {
               src.Add(this.gameObject.AddComponent<AudioSource>());
           }
       }

       /// <summary>
       /// 音频播放,可以选用多种模式进行播放
       /// </summary>
       public AudioSource Play(AudioType type,AudioClip clip = null,string path = "")
       {
           var tempClip = getAudioClip(clip, path);
           if (tempClip == null) { return null; }
           if (clipdata.ContainsKey(path) == false)
           {
               clipdata.Add(path, clip);
           }
           switch (type)
           {
                   //默认为播放背景音乐
               case AudioType.Loop:

                   src[0].audio.clip = tempClip;
                   src[0].audio.loop = true;
                   src[0].volume = vol;
                   src[0].audio.Play();

                   return src[0].audio;

               case AudioType.Once:

                   foreach (var m in src)
                   {
                       if (m.isPlaying == false)
                       {
                           m.audio.clip = tempClip;
                           m.audio.loop = false;
                           m.volume = vol;
                           m.audio.Play();

                           return m.audio;
                       }
                   }
                   return null;

               case AudioType.LoopStop:

                   if (src[0].audio.clip != null)
                   {
                       src[0].audio.Stop();
                       return src[0].audio;
                   }

                   return null;

               case AudioType.LoopContinue:

                   if (src[0].audio.clip != null)
                   {
                       src[0].volume = vol;
                       src[0].audio.Play();
                       return src[0].audio;
                   }

                   return null;

               case AudioType.StopAll:
                   foreach (var m in src)
                   {
                       if (m.isPlaying)
                       {
                           m.Stop();
                       }
                   }

                   return null;
           }

           return null;

       }

       private AudioClip getAudioClip(AudioClip clip = null, string path = "")
       {
           if (clipdata.ContainsKey(path))
           {
               return clipdata[path];
           }

           if (clip == null && path == string.Empty)
           {
               Logger.Log("No AudioClip Can Play");
               return null;
           }
           if (clip != null)
           {
               return clip;
           }
           var data =  ResourceManager.Instance.getResourceData(path,typeof(AudioClip));
           if (data != null)
           {
               return data as AudioClip;
           }
           return null;
       } 




   }
   public enum AudioType
   {
       Loop = 1,
       Once = 2,
       LoopStop = 3,
       LoopContinue = 4,
       StopAll = 5
   }

   /// <summary>
   /// 控件挂
   /// </summary>
   public class AudioPlay : AudioManager
   {
       public AudioClip clip;

       public AudioType type = AudioType.Once;

       private AudioSource souce;

       void Start()
       {
          souce = Play(type, clip);

       }

       /// <summary>
       /// 停止播放
       /// </summary>
       public void Stop()
       {
           if (souce != null) { souce.Stop(); }

       }

       public void Reset()
       {
           souce = Play(type, clip);
       }
   }

}


